package com.umakr.extjstext.tree;

import com.umakr.extjstext.entity.Class;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mikey on 2016/11/14.
 */
public class Tree {
    private String id;
    private String text;
    private List<Tree> children = new ArrayList<>();

    public static List makeTree(List<Class> data){
        List<Tree> trees = new ArrayList<>();
        for(Class c : data){

            if(c.getCp() == null||"".equals(c.getCp())){
                Tree tree = new Tree();
                tree.setId(c.getCid());
                tree.setText(c.getCname());
                findChild(tree,data);
                trees.add(tree);
            }
        }
        return trees;
    }

    public static void findChild(Tree tree,List<Class> data){
        for(Class cc : data){
            if(tree.getId().equals(cc.getCp())){
                Tree tree1 = new Tree();
                tree1.setId(cc.getCid());
                tree1.setText(cc.getCname());
                findChild(tree1,data);
                tree.children.add(tree1);
            }
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public List<Tree> getChildren() {
        return children;
    }

    public void setChildren(List<Tree> children) {
        this.children = children;
    }
}
